<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="商品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类目" prop="categoryId">
        <el-cascader
          v-model="queryParams.categoryId"
          clearable
          :options="categoryOptions"
          :props="{value: 'key', label: 'value', children: 'children', expandTrigger: 'hover'}"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item label="商品状态" prop="itemStatus">
        <el-select v-model="queryParams.itemStatus" clearable size="small">
          <el-option
            v-for="item in itemStatusOptions"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable size="small" style="width: 240px">
          <el-option
            v-for="item in statusOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="createTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          @click="handleOnline"
          v-hasPermi="['system:item:online']"
        >上架</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          @click="handleOffline"
          v-hasPermi="['system:item:offline']"
        >下架</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:item:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange" max-height="600" style="width: 100%">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" >
        <template slot-scope="scope">
          <router-link :to="{ path: '/item/item-info', query: {itemId: scope.row.id} } " class="link-type">
            <span>{{ scope.row.id }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="商品图片" align="center" prop="primaryPic" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <img :src="scope.row.primaryPic"  width="60" height="60" />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="itemName" width="100" :show-overflow-tooltip="true" />
      <el-table-column label="商品简介" align="center" prop="simpleDesc" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="类目" align="center" prop="category" :show-overflow-tooltip="true" />
      <el-table-column label="商品状态" align="center" prop="itemStatus">
        <template slot-scope="scope">
          <el-tag :type="itemStatusTag(scope.row.itemStatus)">{{itemStatusFormat(scope.row.itemStatus)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上架人" align="center" prop="onlineBy" width="60"/>
      <el-table-column label="上架时间" align="center" prop="onlineTime" width="180" />
      <el-table-column label="下架人" align="center" prop="offlineBy" width="60"/>
      <el-table-column label="下架时间" align="center" prop="offlineTime" width="180" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'primary' : 'danger'">{{statusFormat(scope.row.status)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="创建人" align="center" prop="createBy" width="60"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="修改人" align="center" prop="updateBy" width="60"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180" />
      <el-table-column fixed="right" label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:item:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:item:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import { listItem, delItem, onlineItem, offlineItem } from "@/api/item/item";

  export default {
    name: "ItemList",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 商品表格数据
        itemList: [],
        // 状态数据商品
        statusOptions: [],
        itemStatusOptions: [],
        categoryOptions: [],
        createTime: [],
        // 查询参数
        queryParams: {
          page: 1,
          size: 20,
          itemName: undefined,
          categoryId: undefined,
          itemStatus: undefined,
          status: undefined
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
      this.getOptions("item-status").then(response => {
        this.itemStatusOptions = response.data.options;
      });
      this.getOptions("item-category").then(response => {
        this.categoryOptions = response.data.options;
      });
    },
    methods: {
      /** 查询商品类型列表 */
      getList() {
        this.loading = true;
        const categoryId = this.queryParams.categoryId ? this.queryParams.categoryId[1] : null;
        const payload = {...this.queryParams, categoryId: categoryId, createTime: this.parseDataRange(this.createTime)};
        listItem(payload).then(response => {
            this.itemList = response.data.rows;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      // 状态翻译
      itemStatusTag(itemStatus) {
        if (itemStatus === 0) {
          return 'warning';
        } else if (itemStatus === 1) {
          return 'primary';
        } else if (itemStatus === 2) {
          return 'danger';
        }
        return '';
      },
      statusFormat(data) {
        return this.selectDictLabel(this.statusOptions, data);
      },
      itemStatusFormat(data) {
        return this.selectOptionValue(this.itemStatusOptions, data);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.createTime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$router.push('/item/item-add');
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$router.push({path: '/item/item-edit', query: {itemId: row.id}});
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id ? [row.id] : this.ids;
        this.$confirm('是否确认删除商品编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delItem(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 上架按钮操作 */
      handleOnline(row) {
        const ids = row.id ? [row.id] : this.ids;
        this.$confirm('是否确认上架商品"' + ids + '"', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return onlineItem(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("上架成功");
        }).catch(function() {});
      },
      /** 下架按钮操作 */
      handleOffline(row) {
        const ids = row.id ? [row.id] : this.ids;
        this.$confirm('是否确认下架商品"' + ids + '"?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return offlineItem(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("下架成功");
        }).catch(function() {});
      }
    }
  };
</script>
